#include "keyboard.h"
#include "ui_keyboard.h"
#include <QPushButton>
#include <QKeyEvent>
#include <QApplication>
#include <QGridLayout>
#include <QDebug>
#include <QPropertyAnimation>
#include "../model/osdutilsmodel.h"

#define DIGITS  "0123456789"
#define ALPHA   "abcdefghijklmnopqrstuvwxyz"
#define SYMBOL  ".-\\:;()_\"',~*?!@/"
#define OK      "ok"
#define DELETE  "delete"
#define UPPER   "upper"
#define CHANGE  "change"

KeyBoard::KeyBoard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::KeyBoard),
    m_isUpper(false),
    m_isSymbol(false)
{
    m_o = NULL;
    m_currentBoard = 1;

    ui->setupUi(this);
    setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint /*| Qt::Tool*/);//设置为工具模式后，坐标系就变成了global
    initbuttons();
    initkeymapper();
    initFocusOrder();
    m_digitLeft = new QVBoxLayout;
    m_digitRight = new QVBoxLayout;
    m_digitCenter = new QGridLayout;

    m_layout1 = new QHBoxLayout;
    m_layout2 = new QHBoxLayout;
    m_layout3 = new QHBoxLayout;

    m_slayout1 = new QHBoxLayout;
    m_slayout2 = new QHBoxLayout;
    m_slayout3 = new QHBoxLayout;

    m_digitLeft->setSpacing(20);
    m_digitRight->setSpacing(20);
    m_digitCenter->setSpacing(20);
    m_layout1->setSpacing(20);
    m_layout2->setSpacing(20);
    m_layout3->setSpacing(20);

    m_slayout1->setSpacing(20);
    m_slayout2->setSpacing(20);
    m_slayout3->setSpacing(20);

    m_digitLeft->setContentsMargins(0,0,0,0);
    m_digitRight->setContentsMargins(0,0,0,0);
    m_digitCenter->setContentsMargins(0,0,0,0);
    m_layout1->setContentsMargins(0,0,0,0);
    m_layout2->setContentsMargins(0,0,0,0);
    m_layout3->setContentsMargins(0,0,0,0);

    m_slayout1->setContentsMargins(0,0,0,0);
    m_slayout2->setContentsMargins(0,0,0,0);
    m_slayout3->setContentsMargins(0,0,0,0);

    ui->digit_layout->addLayout(m_digitLeft);
    ui->digit_layout->addLayout(m_digitCenter);
    ui->digit_layout->addLayout(m_digitRight);

    m_animation = new QPropertyAnimation(this, "geometry");
    m_animation->setDuration(500);
    m_animation->setEasingCurve(QEasingCurve::InOutQuad);
}

KeyBoard::~KeyBoard()
{
    delete ui;
}

bool KeyBoard::dealButton(QObject *o)
{
    QObject *s = o;
    if (s == m_rbuttons.value(OK))
    {
        m_animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));
        m_animation->setEndValue(QRect(this->x(), this->y()+this->height(), this->width(), this->height()));
        m_animation->start();

        QObject *receiver = (m_o == NULL) ? QApplication::focusWidget():m_o;
        QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Enter,Qt::NoModifier,"");
        QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Enter,Qt::NoModifier,"");
        QApplication::sendEvent(receiver, &keyPress);
        QApplication::sendEvent(receiver, &keyRelease);
        return true;
    }
    else if(s == m_rbuttons.value(UPPER))
    {

        if (m_isUpper)
        {//修改所有按键的显示为小写
            QString str = QString(ALPHA);
            for(int i = 0; i < str.count(); i++)
            {
                QString t = QString(str.at(i));
                m_rbuttons.value(t)->setText(t.toLower());
            }
        }
        else
        {//修改所有按键的显示为大写
            QString str = QString(ALPHA);
            for(int i = 0; i < str.count(); i++)
            {
                QString t = QString(str.at(i));
                m_rbuttons.value(t)->setText(t.toUpper());
            }
        }
        m_isUpper = !m_isUpper;
        return true;
    }
    else if (s ==  m_rbuttons.value(CHANGE))
    {

        if (m_isSymbol)
        {//切换到键盘页面
            setKeyboard(1);
        }
        else
        {//切换到符号页面
            setKeyboard(2);
        }
        return true;
    }
    else
    {
        QPushButton* b = dynamic_cast<QPushButton*>(s);
        if (!b) return false;
        QString txt = b->text();
        if (b == m_rbuttons.value(DELETE))
        {
            txt = DELETE;
        }
        QObject *receiver = (m_o == NULL) ? QApplication::focusWidget():m_o;
        QKeyEvent keyPress(QEvent::KeyPress, m_keyMapper.value(txt),Qt::NoModifier,txt);
        QKeyEvent keyRelease(QEvent::KeyRelease, m_keyMapper.value(txt),Qt::NoModifier,txt);
        QApplication::sendEvent(receiver, &keyPress);
        QApplication::sendEvent(receiver, &keyRelease);
        return true;
    }
    return false;
}

void KeyBoard::slotReceiveKeyClicked()
{
    QObject *s = sender();
    if (s == m_rbuttons.value(OK))
    {
        m_animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));
        m_animation->setEndValue(QRect(this->x(), this->y()+this->height(), this->width(), this->height()));
        m_animation->start();

        QObject *receiver = (m_o == NULL) ? QApplication::focusWidget():m_o;
        QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Enter,Qt::NoModifier,"");
        QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Enter,Qt::NoModifier,"");
        QApplication::sendEvent(receiver, &keyPress);
        QApplication::sendEvent(receiver, &keyRelease);
    }
    else if(s == m_rbuttons.value(UPPER))
    {

        if (m_isUpper)
        {//修改所有按键的显示为小写
            QString str = QString(ALPHA);
            for(int i = 0; i < str.count(); i++)
            {
                QString t = QString(str.at(i));
                m_rbuttons.value(t)->setText(t.toLower());
            }
        }
        else
        {//修改所有按键的显示为大写
            QString str = QString(ALPHA);
            for(int i = 0; i < str.count(); i++)
            {
                QString t = QString(str.at(i));
                m_rbuttons.value(t)->setText(t.toUpper());
            }
        }
        m_isUpper = !m_isUpper;
    }
    else if (s ==  m_rbuttons.value(CHANGE))
    {

        if (m_isSymbol)
        {//切换到键盘页面
            setKeyboard(1);
        }
        else
        {//切换到符号页面
            setKeyboard(2);
        }
    }
    else
    {
        QPushButton* b = dynamic_cast<QPushButton*>(s);
        if (!b) return;
        QString txt = b->text();
        if (b == m_rbuttons.value(DELETE))
        {
            txt = DELETE;
        }
        QObject *receiver = (m_o == NULL) ? QApplication::focusWidget():m_o;
        QKeyEvent keyPress(QEvent::KeyPress, m_keyMapper.value(txt),Qt::NoModifier,txt);
        QKeyEvent keyRelease(QEvent::KeyRelease, m_keyMapper.value(txt),Qt::NoModifier,txt);
        QApplication::sendEvent(receiver, &keyPress);
        QApplication::sendEvent(receiver, &keyRelease);
    }
}

void KeyBoard::initkeymapper()
{
    m_keyMapper.insert("0",Qt::Key_0);
    m_keyMapper.insert("1",Qt::Key_1);
    m_keyMapper.insert("2",Qt::Key_2);
    m_keyMapper.insert("3",Qt::Key_3);
    m_keyMapper.insert("4",Qt::Key_4);
    m_keyMapper.insert("5",Qt::Key_5);
    m_keyMapper.insert("6",Qt::Key_6);
    m_keyMapper.insert("7",Qt::Key_7);
    m_keyMapper.insert("8",Qt::Key_8);
    m_keyMapper.insert("9",Qt::Key_9);

    m_keyMapper.insert("a",Qt::Key_A);
    m_keyMapper.insert("b",Qt::Key_B);
    m_keyMapper.insert("c",Qt::Key_C);
    m_keyMapper.insert("d",Qt::Key_D);
    m_keyMapper.insert("e",Qt::Key_E);
    m_keyMapper.insert("f",Qt::Key_F);
    m_keyMapper.insert("g",Qt::Key_G);
    m_keyMapper.insert("h",Qt::Key_H);
    m_keyMapper.insert("i",Qt::Key_I);
    m_keyMapper.insert("j",Qt::Key_J);
    m_keyMapper.insert("k",Qt::Key_K);
    m_keyMapper.insert("l",Qt::Key_L);
    m_keyMapper.insert("m",Qt::Key_M);
    m_keyMapper.insert("n",Qt::Key_N);
    m_keyMapper.insert("o",Qt::Key_O);
    m_keyMapper.insert("p",Qt::Key_P);
    m_keyMapper.insert("q",Qt::Key_Q);
    m_keyMapper.insert("r",Qt::Key_R);
    m_keyMapper.insert("s",Qt::Key_S);
    m_keyMapper.insert("t",Qt::Key_T);
    m_keyMapper.insert("u",Qt::Key_U);
    m_keyMapper.insert("v",Qt::Key_V);
    m_keyMapper.insert("w",Qt::Key_W);
    m_keyMapper.insert("x",Qt::Key_X);
    m_keyMapper.insert("y",Qt::Key_Y);
    m_keyMapper.insert("z",Qt::Key_Z);

    m_keyMapper.insert("~",Qt::Key_AsciiTilde);
    m_keyMapper.insert("!",Qt::Key_Exclam);
    m_keyMapper.insert("@",Qt::Key_At);
    m_keyMapper.insert("*",Qt::Key_Asterisk);
    m_keyMapper.insert("(",Qt::Key_ParenLeft);
    m_keyMapper.insert(")",Qt::Key_ParenRight);
    m_keyMapper.insert("-",Qt::Key_Minus);
    m_keyMapper.insert("_",Qt::Key_Underscore);
    m_keyMapper.insert("\\",Qt::Key_Backslash);
    m_keyMapper.insert(":",Qt::Key_Colon);
    m_keyMapper.insert(";",Qt::Key_Semicolon);
    m_keyMapper.insert("'",Qt::Key_QuoteLeft);
    m_keyMapper.insert("\"",Qt::Key_QuoteDbl);
    m_keyMapper.insert(",",Qt::Key_Comma);
    m_keyMapper.insert(".",Qt::Key_Period);
    m_keyMapper.insert("?",Qt::Key_Question);
    m_keyMapper.insert("/",Qt::Key_Slash);
    m_keyMapper.insert(DELETE,Qt::Key_Backspace);
}

void KeyBoard::initbuttons()
{
    m_rbuttons.clear();
    //创建数字键
    QString str = QString(DIGITS);
    for(int i = 0; i < str.count(); i++)
    {
        QString t = QString(str.at(i));
        QPushButton *btn = new QPushButton(this);
        btn->hide();
        btn->setText(t);
        btn->setStyleSheet("background:rgba(14,20,45,100%);"
                           "border-bottom:3px solid rgba(28,37,76,100%);"
                           "color:white;"
                           "font-size:38px;"
                           "font-weight:400;");

        btn->setFocusPolicy(Qt::StrongFocus);
        btn->installEventFilter(this);
        connect(btn, SIGNAL(pressed()), this, SLOT(slotReceiveKeyClicked()));
        m_rbuttons.insert(t,btn);
    }
    //创建英文字母
    str = QString(ALPHA);
    for(int i = 0; i < str.count(); i++)
    {
        QString t = QString(str.at(i));
        QPushButton *btn = new QPushButton(this);
        btn->hide();
        btn->setText(t);
        btn->setStyleSheet("background:rgba(14,20,45,100%);"
                           "border-bottom:3px solid rgba(28,37,76,100%);"
                           "color:white;"
                           "font-size:38px;"
                           "font-weight:400;");
        btn->setFocusPolicy(Qt::StrongFocus);
        btn->installEventFilter(this);
        connect(btn, SIGNAL(pressed()), this, SLOT(slotReceiveKeyClicked()));
        m_rbuttons.insert(t,btn);
    }
    //创建符号表
    str = QString(SYMBOL);
    for(int i = 0; i < str.count(); i++)
    {
        QString t = QString(str.at(i));
        QPushButton *btn = new QPushButton(this);
        btn->hide();
        btn->setText(t);
        btn->setStyleSheet("background:rgba(14,20,45,100%);"
                           "border-bottom:3px solid rgba(28,37,76,100%);"
                           "color:white;"
                           "font-size:38px;"
                           "font-weight:400;");
        btn->setFocusPolicy(Qt::StrongFocus);
        btn->installEventFilter(this);
        connect(btn, SIGNAL(pressed()), this, SLOT(slotReceiveKeyClicked()));
        m_rbuttons.insert(t,btn);
    }
    //创建功能键
    QPushButton *okbtn = new QPushButton(this);
    okbtn->hide();
    okbtn->setText("确定");
    okbtn->setStyleSheet("background:rgba(51, 62, 106, 100%);"
                       "border-bottom:3px solid rgba(51,62,106,100%);"
                         "color:white;"
                         "font-size:38px;"
                         "font-weight:400;");

    okbtn->setFocusPolicy(Qt::StrongFocus);
    okbtn->installEventFilter(this);
    connect(okbtn, SIGNAL(pressed()), this, SLOT(slotReceiveKeyClicked()));
    m_rbuttons.insert(OK,okbtn);

    QPushButton *deletebtn = new QPushButton(this);
    deletebtn->hide();
    deletebtn->setStyleSheet("background:rgba(51, 62, 106, 100%);"
                       "border-bottom:3px solid rgba(51,62,106,100%);"
                             "font-size:38px;"
                             "font-weight:400;"
                       );
    deletebtn->setFocusPolicy(Qt::StrongFocus);
    deletebtn->installEventFilter(this);

    connect(deletebtn, SIGNAL(pressed()), this, SLOT(slotReceiveKeyClicked()));
    m_rbuttons.insert(DELETE,deletebtn);

    QPushButton *upperbtn = new QPushButton(this);
    upperbtn->hide();
    upperbtn->setStyleSheet("background:rgba(14,20,45,100%);"
                       "border-bottom:3px solid rgba(28,37,76,100%);"
                            "color:white;"
                            "font-size:38px;"
                            "font-weight:400;");

    upperbtn->setFocusPolicy(Qt::StrongFocus);
    upperbtn->installEventFilter(this);
    connect(upperbtn, SIGNAL(pressed()), this, SLOT(slotReceiveKeyClicked()));
    m_rbuttons.insert(UPPER,upperbtn);

    QPushButton *changebtn = new QPushButton(this);
    changebtn->hide();
    changebtn->setStyleSheet("background:rgba(14,20,45,100%);"
                       "border-bottom:3px solid rgba(28,37,76,100%);"
                       "color:white;"
                             "font-size:38px;"
                             "font-weight:400;");
    changebtn->setFocusPolicy(Qt::StrongFocus);
    changebtn->installEventFilter(this);
    changebtn->setText("符");
    connect(changebtn, SIGNAL(pressed()), this, SLOT(slotReceiveKeyClicked()));
    m_rbuttons.insert(CHANGE,changebtn);
}


void KeyBoard::initDigitsBoard()
{
    cleanDigitLayout();

    m_digitLeft->addWidget(m_rbuttons.value("."));
    m_digitLeft->addWidget(m_rbuttons.value(DELETE));

    m_digitRight->addWidget(m_rbuttons.value("0"));
    m_digitRight->addWidget(m_rbuttons.value(OK));

    m_digitCenter->addWidget(m_rbuttons.value("1"),0,0);
    m_digitCenter->addWidget(m_rbuttons.value("2"),0,1);
    m_digitCenter->addWidget(m_rbuttons.value("3"),0,2);

    m_digitCenter->addWidget(m_rbuttons.value("4"),1,0);
    m_digitCenter->addWidget(m_rbuttons.value("5"),1,1);
    m_digitCenter->addWidget(m_rbuttons.value("6"),1,2);

    m_digitCenter->addWidget(m_rbuttons.value("7"),2,0);
    m_digitCenter->addWidget(m_rbuttons.value("8"),2,1);
    m_digitCenter->addWidget(m_rbuttons.value("9"),2,2);

    m_rbuttons.value(".")->setMinimumSize(200,276);
    m_rbuttons.value(".")->setMaximumSize(200,276);
    m_rbuttons.value(DELETE)->setMinimumSize(200,128);
    m_rbuttons.value(DELETE)->setMaximumSize(200,128);
    m_rbuttons.value(OK)->setMinimumSize(200,128);
    m_rbuttons.value(OK)->setMaximumSize(200,128);

    QString str = QString(DIGITS);
    for(int i = 0; i < str.count(); i++)
    {
        QString t = QString(str.at(i));
        m_rbuttons.value(t)->setMinimumSize(446,128);
        m_rbuttons.value(t)->setMaximumSize(446,128);
        m_rbuttons.value(t)->show();
    }

    m_rbuttons.value("0")->setMinimumSize(200,276);
    m_rbuttons.value("0")->setMaximumSize(200,276);
    m_rbuttons.value(".")->show();
    m_rbuttons.value(DELETE)->show();
    m_rbuttons.value(OK)->show();

    ui->stackedWidget->setCurrentIndex(0);

    m_rbuttons.value(".")->setFocus();
}

void KeyBoard::initAlphaBoard()
{

    cleanLayout123();

    ui->alpha_layout->addLayout(m_layout1);
    ui->alpha_layout->addLayout(m_layout2);
    ui->alpha_layout->addLayout(m_layout3);

    m_layout1->addWidget(m_rbuttons.value("q"));
    m_layout1->addWidget(m_rbuttons.value("w"));
    m_layout1->addWidget(m_rbuttons.value("e"));
    m_layout1->addWidget(m_rbuttons.value("r"));
    m_layout1->addWidget(m_rbuttons.value("t"));
    m_layout1->addWidget(m_rbuttons.value("y"));
    m_layout1->addWidget(m_rbuttons.value("u"));
    m_layout1->addWidget(m_rbuttons.value("i"));
    m_layout1->addWidget(m_rbuttons.value("o"));
    m_layout1->addWidget(m_rbuttons.value("p"));


    m_layout2->addWidget(m_rbuttons.value(UPPER));
    m_layout2->addWidget(m_rbuttons.value("a"));
    m_layout2->addWidget(m_rbuttons.value("s"));
    m_layout2->addWidget(m_rbuttons.value("d"));
    m_layout2->addWidget(m_rbuttons.value("f"));
    m_layout2->addWidget(m_rbuttons.value("g"));
    m_layout2->addWidget(m_rbuttons.value("h"));
    m_layout2->addWidget(m_rbuttons.value("j"));
    m_layout2->addWidget(m_rbuttons.value("k"));
    m_layout2->addWidget(m_rbuttons.value("l"));
    m_layout2->addWidget(m_rbuttons.value(CHANGE));
    m_rbuttons.value(CHANGE)->setText("符");
    m_isSymbol = false;

    m_layout3->addWidget(m_rbuttons.value(DELETE));

    m_layout3->addWidget(m_rbuttons.value("z"));
    m_layout3->addWidget(m_rbuttons.value("x"));
    m_layout3->addWidget(m_rbuttons.value("c"));
    m_layout3->addWidget(m_rbuttons.value("v"));
    m_layout3->addWidget(m_rbuttons.value("b"));
    m_layout3->addWidget(m_rbuttons.value("n"));
    m_layout3->addWidget(m_rbuttons.value("m"));

    m_layout3->addWidget(m_rbuttons.value(OK));

    m_rbuttons.value(DELETE)->setMinimumSize(164+92,128);
    m_rbuttons.value(DELETE)->setMaximumSize(164+92,128);
    m_rbuttons.value(OK)->setMinimumSize(164+92,128);
    m_rbuttons.value(OK)->setMaximumSize(164+92,128);
    m_rbuttons.value(CHANGE)->setMinimumSize(72,128);
    m_rbuttons.value(CHANGE)->setMaximumSize(72,128);
    m_rbuttons.value(UPPER)->setMinimumSize(72,128);
    m_rbuttons.value(UPPER)->setMaximumSize(72,128);

    QString str = QString(ALPHA);
    for(int i = 0; i < str.count(); i++)
    {
        QString t = QString(str.at(i));
        m_rbuttons.value(t)->setMinimumSize(164,128);
        m_rbuttons.value(t)->setMaximumSize(164,128);
        m_rbuttons.value(t)->show();
    }

    m_rbuttons.value(UPPER)->show();
    m_rbuttons.value(CHANGE)->show();
    m_rbuttons.value(DELETE)->show();
    m_rbuttons.value(OK)->show();

    ui->stackedWidget->setCurrentIndex(1);
}

void KeyBoard::initSymbolBoard()
{
    cleanSLayout123();

    ui->symbol_layout->addLayout(m_slayout1);
    ui->symbol_layout->addLayout(m_slayout2);
    ui->symbol_layout->addLayout(m_slayout3);

    m_slayout1->addWidget(m_rbuttons.value("0"));
    m_slayout1->addWidget(m_rbuttons.value("1"));
    m_slayout1->addWidget(m_rbuttons.value("2"));
    m_slayout1->addWidget(m_rbuttons.value("3"));
    m_slayout1->addWidget(m_rbuttons.value("4"));
    m_slayout1->addWidget(m_rbuttons.value("5"));
    m_slayout1->addWidget(m_rbuttons.value("6"));
    m_slayout1->addWidget(m_rbuttons.value("7"));
    m_slayout1->addWidget(m_rbuttons.value("8"));
    m_slayout1->addWidget(m_rbuttons.value("9"));

    m_slayout2->addWidget(m_rbuttons.value("."));
    m_slayout2->addWidget(m_rbuttons.value("-"));
    m_slayout2->addWidget(m_rbuttons.value("\\"));
    m_slayout2->addWidget(m_rbuttons.value(":"));
    m_slayout2->addWidget(m_rbuttons.value(";"));
    m_slayout2->addWidget(m_rbuttons.value("("));
    m_slayout2->addWidget(m_rbuttons.value(")"));
    m_slayout2->addWidget(m_rbuttons.value("_"));
    m_slayout2->addWidget(m_rbuttons.value("\""));
    m_slayout2->addWidget(m_rbuttons.value("'"));
    m_slayout2->addWidget(m_rbuttons.value(CHANGE));
    m_rbuttons.value(CHANGE)->setText("英");
    m_isSymbol = true;

    m_slayout3->addWidget(m_rbuttons.value(DELETE));


    m_slayout3->addWidget(m_rbuttons.value(","));
    m_slayout3->addWidget(m_rbuttons.value("~"));
    m_slayout3->addWidget(m_rbuttons.value("*"));
    m_slayout3->addWidget(m_rbuttons.value("?"));
    m_slayout3->addWidget(m_rbuttons.value("!"));
    m_slayout3->addWidget(m_rbuttons.value("@"));
    m_slayout3->addWidget(m_rbuttons.value("/"));

    m_slayout3->addWidget(m_rbuttons.value(OK));

    m_rbuttons.value(DELETE)->setMinimumSize(164+92,128);
    m_rbuttons.value(DELETE)->setMaximumSize(164+92,128);
    m_rbuttons.value(OK)->setMinimumSize(164+92,128);
    m_rbuttons.value(OK)->setMaximumSize(164+92,128);
    m_rbuttons.value(CHANGE)->setMinimumSize(72,128);
    m_rbuttons.value(CHANGE)->setMaximumSize(72,128);

    QString str = QString(SYMBOL);
    for(int i = 0; i < str.count(); i++)
    {
        QString t = QString(str.at(i));
        m_rbuttons.value(t)->setMinimumSize(164,128);
        m_rbuttons.value(t)->setMaximumSize(164,128);
    }

    m_rbuttons.value(".")->setMinimumSize(72,128);
    m_rbuttons.value(".")->setMaximumSize(72,128);

    str = QString(DIGITS);
    for(int i = 0; i < str.count(); i++)
    {
        QString t = QString(str.at(i));
        m_rbuttons.value(t)->setMinimumSize(164,128);
        m_rbuttons.value(t)->setMaximumSize(164,128);
    }

    str = QString(SYMBOL);
    for(int i = 0; i < str.count(); i++)
    {
        QString t = QString(str.at(i));
        m_rbuttons.value(t)->show();
    }
    str = QString(DIGITS);
    for(int i = 0; i < str.count(); i++)
    {
        QString t = QString(str.at(i));
        m_rbuttons.value(t)->show();
    }

    m_rbuttons.value(CHANGE)->show();
    m_rbuttons.value(DELETE)->show();
    m_rbuttons.value(OK)->show();

    ui->stackedWidget->setCurrentIndex(2);
}

void KeyBoard::initFocusOrder()
{
    QList<QObject *> orderlist;
    orderlist.clear();
    orderlist<< NULL << m_rbuttons.value(DELETE) << NULL << m_rbuttons.value("1");
    m_ctrlOrder.insert(m_rbuttons.value("."), orderlist);

    orderlist.clear();
    orderlist<< m_rbuttons.value(".") << NULL << NULL << m_rbuttons.value("7");
    m_ctrlOrder.insert(m_rbuttons.value(DELETE), orderlist);

    orderlist.clear();
    orderlist<< NULL << m_rbuttons.value(OK) << m_rbuttons.value("3") << NULL;
    m_ctrlOrder.insert(m_rbuttons.value("0"), orderlist);

    orderlist.clear();
    orderlist<< m_rbuttons.value("0") << NULL << m_rbuttons.value("9") << NULL;
    m_ctrlOrder.insert(m_rbuttons.value(OK), orderlist);

    orderlist.clear();
    orderlist<< NULL << m_rbuttons.value("4") << m_rbuttons.value(".") << m_rbuttons.value("2");
    m_ctrlOrder.insert(m_rbuttons.value("1"), orderlist);

    orderlist.clear();
    orderlist<< NULL << m_rbuttons.value("5") << m_rbuttons.value("1") << m_rbuttons.value("3");
    m_ctrlOrder.insert(m_rbuttons.value("2"), orderlist);

    orderlist.clear();
    orderlist<< NULL << m_rbuttons.value("6") << m_rbuttons.value("2") << m_rbuttons.value("0");
    m_ctrlOrder.insert(m_rbuttons.value("3"), orderlist);

    orderlist.clear();
    orderlist<< m_rbuttons.value("1") << m_rbuttons.value("7") << m_rbuttons.value(".") << m_rbuttons.value("5");
    m_ctrlOrder.insert(m_rbuttons.value("4"), orderlist);

    orderlist.clear();
    orderlist<< m_rbuttons.value("2") << m_rbuttons.value("8") << m_rbuttons.value("4") << m_rbuttons.value("6");
    m_ctrlOrder.insert(m_rbuttons.value("5"), orderlist);

    orderlist.clear();
    orderlist<< m_rbuttons.value("3") << m_rbuttons.value("9") << m_rbuttons.value("8") << m_rbuttons.value("0");
    m_ctrlOrder.insert(m_rbuttons.value("6"), orderlist);

    orderlist.clear();
    orderlist<< m_rbuttons.value("4") << NULL << m_rbuttons.value(DELETE) << m_rbuttons.value("8");
    m_ctrlOrder.insert(m_rbuttons.value("7"), orderlist);

    orderlist.clear();
    orderlist<< m_rbuttons.value("5") << NULL << m_rbuttons.value("7") << m_rbuttons.value("9");
    m_ctrlOrder.insert(m_rbuttons.value("8"), orderlist);

    orderlist.clear();
    orderlist<< m_rbuttons.value("6") << NULL << m_rbuttons.value("8") << m_rbuttons.value(OK);
    m_ctrlOrder.insert(m_rbuttons.value("9"), orderlist);
}

bool KeyBoard::eventFilter(QObject *o, QEvent *e)
{
    if (e->type() == QEvent::KeyRelease)
    {
        QKeyEvent *ke = dynamic_cast<QKeyEvent*>(e);
        if (ke && (ke->key() == Qt::Key_Right
                   || ke->key() == Qt::Key_Down
                   || ke->key() == Qt::Key_Left
                   || ke->key() == Qt::Key_Up))
        {
            if (!m_ctrlOrder.contains(o))
            {
                return false;
            }

            QObject *target = m_ctrlOrder.value(o).at(OSDUtilsModel::instance().keyIndex(ke->key()));
            if (target)
            {
                QWidget* w = dynamic_cast<QWidget*>(target);
                if (w) w->setFocus();
            }
            return true;
        } else if(ke->key() == Qt::Key_Escape)
        {
            m_animation->setStartValue(QRect(this->x(), this->y(), this->width(), this->height()));
            m_animation->setEndValue(QRect(this->x(), this->y()+this->height(), this->width(), this->height()));
            m_animation->start();

            QObject *receiver = (m_o == NULL) ? QApplication::focusWidget():m_o;
            QKeyEvent keyPress(QEvent::KeyPress, Qt::Key_Enter,Qt::NoModifier,"");
            QKeyEvent keyRelease(QEvent::KeyRelease, Qt::Key_Enter,Qt::NoModifier,"");
            QApplication::sendEvent(receiver, &keyPress);
            QApplication::sendEvent(receiver, &keyRelease);
            return true;
        }
    }
    else if (e->type() == QEvent::KeyPress)
    {
        return true;
    }
    else if (e->type() == QEvent::MouseButtonRelease)
    {
        return dealButton(o);
    }
    return false;
}

void KeyBoard::clearLayout(QLayout *layout)
{
    QObjectList objs = layout->children();
    foreach(QObject *o, objs)
    {
        QPushButton *b = dynamic_cast<QPushButton *>(o);
        if (b)
        {
            b->setVisible(false);
            layout->removeWidget(b);
            b->setParent(0);
        }
    }
}

void KeyBoard::cleanLayout123()
{
    m_layout1->setParent(0);
    m_layout2->setParent(0);
    m_layout3->setParent(0);
    clearLayout(m_layout1);
    clearLayout(m_layout2);
    clearLayout(m_layout3);
}

void KeyBoard::cleanSLayout123()
{
    m_slayout1->setParent(0);
    m_slayout2->setParent(0);
    m_slayout3->setParent(0);
    clearLayout(m_slayout1);
    clearLayout(m_slayout2);
    clearLayout(m_slayout3);
}


void KeyBoard::cleanDigitLayout()
{

    clearLayout(m_digitLeft);
    clearLayout(m_digitRight);
    clearLayout(m_digitCenter);
}

void KeyBoard::setShowKeyBoard(QObject *o, int type)
{
    m_o = o;
    m_currentBoard = type;
    setKeyboard(m_currentBoard);
}


void KeyBoard::setKeyboard(int type)
{
    switch(type){
    case 0:
        initDigitsBoard();
        break;
    case 1:
        initAlphaBoard();
        break;
    case 2:
        initSymbolBoard();
        break;
    default:
        break;
    }
}









































